package com.example.neusoft.yuejian_12.Model;

import com.example.neusoft.yuejian_12.Activity.PhotoInfoActivity;
import com.example.neusoft.yuejian_12.Interface.PhotoFace;
import com.example.neusoft.yuejian_12.JavaBean.Photo;
import com.example.neusoft.yuejian_12.Listener.PhotoCommentListener;
import com.example.neusoft.yuejian_12.Listener.PhotoInfoListener;
import com.example.neusoft.yuejian_12.Listener.PhotoInsertListener;
import com.example.neusoft.yuejian_12.Listener.PhotoListener;
import com.example.neusoft.yuejian_12.Service.PhotoService;

import java.io.File;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 晓真 on 2017/9/30.
 */

public class PhotoModel  implements PhotoFace {
    private Retrofit retrofit;
    private PhotoService photoService;
    /*private static final String BASE_URL = "http://172.24.10.175/workout/api.php/";//主机地址*/
    public PhotoModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getPhotoList(String id, String title,final PhotoListener listListener) {
        photoService = retrofit.create(PhotoService.class);
        Call<List<Photo>> call =photoService.photolist();
        call.enqueue(new Callback<List<Photo>>() {
            @Override
            public void onResponse(Call<List<Photo>> call, Response<List<Photo>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("fail");
                }
            }

            @Override
            public void onFailure(Call<List<Photo>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

    @Override
    public void getPhotoByID(String id,final PhotoInfoListener listListener) {
        photoService = retrofit.create(PhotoService.class);
        Call<Photo> call =photoService.getPhotoById(id);
        call.enqueue(new Callback<Photo>() {
            @Override
            public void onResponse(Call<Photo> call, Response<Photo> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("fail");
                }
            }

            @Override
            public void onFailure(Call<Photo> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

    @Override
    public void insertComment(String shaitu_id, String userid, String reply_userid, String reply, final PhotoCommentListener listListener) {
        photoService = retrofit.create(PhotoService.class);
        Call<Integer> call =photoService.insertComment(shaitu_id,userid,reply_userid,reply);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                 if (response.body() > 0) {
                     listListener.onResponse(response.body());
                 } else {
                     listListener.onFail("fail");
                 }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

//    @Override
//    public void push(String userid, String title, String filename, final PhotoInsertListener listListener) {
//        photoService = retrofit.create(PhotoService.class);
//        final File file = new File(String.valueOf(filename));
//        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
//        MultipartBody.Part body = MultipartBody.Part.createFormData("pic", file.getName(),requestBody);
//        Call<Integer> call =photoService.signpic(userid,title,body);
//        call.enqueue(new Callback<Integer>() {
//            @Override
//            public void onResponse(Call<Integer> call, Response<Integer> response) {
//                if (response.body()>0) {
//                    listListener.onResponse(response.body());
//                } else {
//                    listListener.onFail("fail");
//                }
//            }
//
//            @Override
//            public void onFailure(Call<Integer> call, Throwable t) {
//                listListener.onFail(t.toString());
//            }
//        });
//    }


    //发布晒图
    public Call<okhttp3.ResponseBody> signPic(String userid, String title, MultipartBody.Part file){
        PhotoService actImpls = retrofit.create(PhotoService.class);
        return  actImpls.signpic(userid,title,file);
    }

}
